package face.pay.web.device.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import face.pay.constant.UserType;
import face.pay.dao.DeviceApplyDetailDao;
import face.pay.dao.DeviceApplyInfoDao;
import face.pay.dao.DeviceOrderInfoDao;
import face.pay.entity.DeviceApplyInfo;
import face.pay.entity.DeviceOrderInfo;
import face.pay.entity.UserInfo;
import face.pay.util.ResultMessage;
import face.pay.util.page.PageData;
import face.pay.util.page.PageParam;
import face.pay.web.device.service.DeviceApplyInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname DeviceApplyInfoServiceImpl
 * @Description JJL
 * @Date 2019/11/13 17:22
 */
@Service
@Slf4j
public class DeviceApplyInfoServiceImpl implements DeviceApplyInfoService {

    @Autowired
    private DeviceApplyInfoDao deviceApplyInfoDao;

    @Autowired
    private DeviceOrderInfoDao deviceOrderInfoDao;

    @Autowired
    private DeviceApplyDetailDao deviceApplyDetailDao;



    /**
     * 新增代理商申请设备记录
     *
     * @param deviceApplyInfo
     */
    @Override
    public ResultMessage addDeviceApplyInfo(UserInfo loginUser, DeviceApplyInfo deviceApplyInfo) {
        deviceApplyInfo.setOemId(loginUser.getOemId());
        deviceApplyInfo.setUserId(loginUser.getId());
        deviceApplyInfo.setCreatedBy(loginUser.getId() + "");
        if (deviceApplyInfo.getWCount() == null) {
            deviceApplyInfo.setWCount(0);
        }
        if (deviceApplyInfo.getACount() == null) {
            deviceApplyInfo.setACount(0);
        }
        log.info("代理商："+loginUser.getId()+"发起设备申请");
        log.info("微信申请数量："+deviceApplyInfo.getWCount());
        log.info("支付宝申请数量："+deviceApplyInfo.getACount());
        deviceApplyInfoDao.insert(deviceApplyInfo);
        return ResultMessage.createSuccess();
    }

    /**
     * oem查询代理商的设备申请记录
     *
     * @param loginUser
     * @param pageParam
     * @return
     */
    @Override
    public PageData queryPage(UserInfo loginUser, PageParam pageParam, DeviceApplyInfo deviceApplyInfo) {
        PageHelper.startPage(pageParam.getPageNo(), pageParam.getPageSize());

        deviceApplyInfo.setOemId(loginUser.getOemId());

        String  userType = loginUser.getType();
        if(!UserType.OEM.equals(userType)){
            deviceApplyInfo.setUserId(loginUser.getId());
        }

        List<DeviceApplyInfo> oemList = this.deviceApplyInfoDao.queryList(deviceApplyInfo);

        PageInfo<DeviceApplyInfo> pageInfo = new PageInfo<>(oemList);

        PageData<DeviceApplyInfo> pageData = new PageData<>(pageInfo.getTotal(), oemList);

        return pageData;
    }

    /**
     * 设备申请审批  申请驳回
     *
     * @param deviceApplyInfo
     * @return
     */
    @Override
    public ResultMessage rejectApplyInfo(UserInfo loginUser, DeviceApplyInfo deviceApplyInfo) {
        deviceApplyInfo.setUpdatedBy(loginUser.getId() + "");
        deviceApplyInfo.setWActualCount(0);
        deviceApplyInfo.setAActualCount(0);
        deviceApplyInfoDao.update(deviceApplyInfo);
        return ResultMessage.createSuccess();
    }

    /**
     * 设备申请审批   确认划拨
     *
     * @param loginUser
     * @param transferDeviceInfo value: 划拨的设备id, title: 划拨设备类型与设备sn  格式如：  微信:sn
     * @param applyId            设备申请记录id
     * @param userId             设备申请人id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage transferDeviceInfo(UserInfo loginUser, ArrayList<DeviceApplyInfo> transferDeviceInfo,
                                            Integer applyId,
                                            Integer userId) {

        log.info("本次划拨设备总数量" + transferDeviceInfo.size());

        int wx = 0;
        int zfb = 0;
        List<Integer> deviceOrderIds = new ArrayList<>(); //设备id

        if (transferDeviceInfo != null) {
            for (int i = 0; i < transferDeviceInfo.size(); i++) {
                String[] split = transferDeviceInfo.get(i).getTitle().split(":");
                deviceOrderIds.add(transferDeviceInfo.get(i).getValue());
                if ("微信".equals(split[0])) {
                    wx++;
                } else {
                    zfb++;
                }
            }
            if (applyId != null) {
                deviceApplyDetailDao.insert(transferDeviceInfo,applyId,loginUser.getId()); //将记录添加至关联表中

                //设备申请记录
                DeviceApplyInfo deviceApplyInfo = new DeviceApplyInfo();
                deviceApplyInfo.setId(applyId); //设备申请记录id
                deviceApplyInfo.setWActualCount(wx);
                deviceApplyInfo.setAActualCount(zfb);
                deviceApplyInfo.setState(1);
                deviceApplyInfo.setUpdatedBy(loginUser.getId()+"");
                log.info("微信设备数量:"+wx);
                log.info("支付宝设备数量:"+zfb);
                deviceApplyInfoDao.update(deviceApplyInfo);

                //设备目前拥有人改动
                deviceOrderInfoDao.update(userId,deviceOrderIds);
                return ResultMessage.createSuccess();
            }
            return ResultMessage.createFail("申请记录不存在，请刷新页面");
        }
        return ResultMessage.createFail("未选择设备");
    }

    /**
     * 设备申请审批   暂存数据
     *
     * @param loginUser
     * @param transferDeviceInfos value: 划拨的设备id, title: 划拨设备类型与设备sn  格式如：  微信:sn
     * @param applyId            设备申请记录id
     * @return
     */
    @Override
    public ResultMessage depositDeviceInfo(UserInfo loginUser, ArrayList<DeviceApplyInfo> transferDeviceInfos, Integer applyId) {

        if (transferDeviceInfos.size() == 0) {
            return ResultMessage.createFail("请选择数据");
        }
        deviceApplyDetailDao.insert(transferDeviceInfos,applyId,loginUser.getId());
        return ResultMessage.createSuccess();
    }

    @Override
    public ResultMessage queryUsesOperDevices(UserInfo loginUser,Integer applyId) {

        Integer userId = loginUser.getId();

        Map<String,Object> data  = new HashMap<>();

        /**
         * 当前设备申请单 下的设备
         */
        List<DeviceOrderInfo> applyDevices = this.deviceApplyDetailDao.queryOrdersByApplyId(applyId);
        /**
         * 当前用户可以调配的设备
         */
        List<DeviceOrderInfo> userOperDevices  = deviceOrderInfoDao.getUserNotApplyDevices(userId);

        List<Integer> orderIds = new ArrayList<>();
        for (DeviceOrderInfo applyDevice: applyDevices) {
            orderIds.add(applyDevice.getId());
        }

        for (DeviceOrderInfo applyDevice:applyDevices) {
            userOperDevices.add(applyDevice);
        }

        data.put("applyId",applyId);
        data.put("orderIds",orderIds);
        data.put("userOperDevices",userOperDevices);
        return ResultMessage.createSuccess(data);
    }

    /**
     * 划拨完成数据详情
     *
     * @param loginUser
     * @param applyId
     * @return
     */
    @Override
    public ResultMessage detail(UserInfo loginUser, Integer applyId) {
        List<DeviceOrderInfo> userApplyDetailInfo = deviceApplyDetailDao.getUserApplyDetailInfo(applyId);
        return ResultMessage.createSuccess(userApplyDetailInfo);
    }
}
